<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Address extends Base{
	//获取省
	public function province()
	{
		$res=Db::name('city')->where('level',1)->select();
		if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '获取失败！']);
        }
	}
	//获取市、区
	public function cityDistrict()
	{
		if(!empty(input("param.parentId"))){
            $parentId=input("param.parentId");
        }else{
            return json(['code' => 501, 'msg' => '父节点呢？']);
        }
		$res=Db::name('city')->where('parentId',$parentId)->select();
		if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '获取失败！']);
        }
	}
    //是否存在地址，有则返回地址
    public function isAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('address')->where('user_id',$uid)->where('state','<',3)->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '不存在地址！']);
        }
    }
    //获取默认地址
    public function getDefaultAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('address')->where('user_id',$uid)->where('state',1)->find();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '不存在地址！']);
        }
    }
    //获取某条地址
    public function getAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '请输入地址id']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('address')->where('user_id',$uid)->where('id',$id)->where('state','<',3)->find();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '不存在！']);
        }
    }
    //添加用户地址
    public function addAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.name"))){
            $data['name']=input("param.name");
        }else{
            return json(['code' => 502, 'msg' => '请输入姓名']);
        }
        if(!empty(input("param.phone"))){
            $data['phone']=input("param.phone");
        }else{
            return json(['code' => 503, 'msg' => '请输入电话']);
        }
        if(!empty(input("param.address"))){
            $data['address']=input("param.address");
        }else{
            return json(['code' => 504, 'msg' => '请输入地址']);
        }
        if(!empty(input("param.province"))){
            $data['province']=input("param.province");
        }else{
            return json(['code' => 505, 'msg' => '请选择省']);
        }
        if(!empty(input("param.city"))){
            $data['city']=input("param.city");
        }else{
            return json(['code' => 506, 'msg' => '请选择市']);
        }
        if(!empty(input("param.district"))){
            $data['district']=input("param.district");
        // }else{
        //     return json(['code' => 510, 'msg' => '请选择区']);
        }
        if(!empty(input("param.area_code"))){
            $data['area_code']=input("param.area_code");
        }else{
            return json(['code' => 507, 'msg' => '缺少地区编码']);
        }
        if(!empty(input("param.state"))){
            $data['state']=input("param.state");
        }else{
            return json(['code' => 508, 'msg' => '请选择选中(默认)状态']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        if ($data['state']==1) {
        	Db::name('address')->where('user_id',$uid)->update(['state'=>2]);
	        $data['user_id']=$uid;
	        $data['time']=time();
	        $data['uptime']=time();
	        $addresss=Db::name('address')->insert($data);
	        if($addresss){
	            return json(['code' => 200, 'msg' => '成功保存！']);
	        }else{
	            return json(['code' => 500, 'msg' => '保存失败！']);
	        }
        }else{
	        $data['user_id']=$uid;
	        $data['time']=time();
	        $data['uptime']=time();
	        $addresss=Db::name('address')->insert($data);
	        if($addresss){
	            return json(['code' => 200, 'msg' => '成功保存！']);
	        }else{
	            return json(['code' => 500, 'msg' => '保存失败！']);
	        }
        }
    }
    //修改用户地址
    public function editAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.name"))){
            $data['name']=input("param.name");
        }else{
            return json(['code' => 502, 'msg' => '请输入姓名']);
        }
        if(!empty(input("param.phone"))){
            $data['phone']=input("param.phone");
        }else{
            return json(['code' => 503, 'msg' => '请输入电话']);
        }
        if(!empty(input("param.address"))){
            $data['address']=input("param.address");
        }else{
            return json(['code' => 504, 'msg' => '请输入地址']);
        }
        if(!empty(input("param.province"))){
            $data['province']=input("param.province");
        }else{
            return json(['code' => 505, 'msg' => '请选择省']);
        }
        if(!empty(input("param.city"))){
            $data['city']=input("param.city");
        }else{
            return json(['code' => 506, 'msg' => '请选择市']);
        }
        if(!empty(input("param.district"))){
            $data['district']=input("param.district");
        // }else{
        //     return json(['code' => 510, 'msg' => '请选择区']);
        }
        if(!empty(input("param.area_code"))){
            $data['area_code']=input("param.area_code");
        }else{
            return json(['code' => 507, 'msg' => '缺少地区编码']);
        }
        if(!empty(input("param.state"))){
            $data['state']=input("param.state");
        }else{
            return json(['code' => 508, 'msg' => '请选择选中(默认)状态']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 509, 'msg' => '请给出要修改的地址id']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        if ($data['state']==1) {
        	Db::name('address')->where('user_id',$uid)->update(['state'=>2]);
	        $data['uptime']=time();
	        $addresss=Db::name('address')->where('id',$id)->where('user_id',$uid)->update($data);
	        if($addresss){
	            return json(['code' => 200, 'msg' => '修改成功！']);
	        }else{
	            return json(['code' => 500, 'msg' => '修改失败！']);
	        }
        }else{
	        $data['uptime']=time();
	        $addresss=Db::name('address')->where('id',$id)->where('user_id',$uid)->update($data);
	        if($addresss){
	            return json(['code' => 200, 'msg' => '修改成功！']);
	        }else{
	            return json(['code' => 500, 'msg' => '修改失败！']);
	        }
        }
    }
    //选中默认地址
    public function selectAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '请输入需要默认的地址id']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        Db::name('address')->where('user_id',$uid)->update(['state'=>2]);
        $resid=Db::name('address')->where('user_id',$uid)->where('id',$id)->update(['state'=>1]);
        if($resid){
            return json(['code' => 200, 'msg' => '成功！']);
        }else{
            return json(['code' => 500, 'msg' => '失败！']);
        }
    }
    //删除地址
    public function deleteAddress(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '请输入需要删除的地址id']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('address')->where('user_id',$uid)->where('id',$id)->update(['state'=>3]);
        if($res){
            return json(['code' => 200, 'msg' => '成功！']);
        }else{
            return json(['code' => 500, 'msg' => '失败！']);
        }
    }
}